<?php


namespace app\appapi\controller;


use cmf\controller\RestBaseController;
use cmf\controller\RestUserBaseController;
use PhpOffice\PhpSpreadsheet\IOFactory;

class ExcelController extends RestBaseController
{
    public function excel_upload()
    {
        $res = $this->uploads('file', './upload/excel');
        if ($res['code'] == 0) {
            $this->error($res['msg']);
        }
        $this->success($res['msg']);
    }

//    source: 2
//    type: 1
//    screen: 0
//    excel_file_path: /Uploads/excel/2020_06_29/15934170774095.xlsx
    public function import_excel($type = 1, $excel_file_path = '/upload/excel/20200629/ad8df54e46cf703424b5492f4c8ce04c.xlsx')
    {
        $excel_type = ucfirst(explode('.', $excel_file_path)[1]);
        $reader = IOFactory::createReader($excel_type);
        $phpExcel = $reader->load(WEB_ROOT . $excel_file_path);
        $sheet = $phpExcel->getSheet(0);
        $highestRow = $sheet->getHighestRow();
        $highestColumn = $sheet->getHighestColumn();

        $data = [];
        if ($highestColumn == 'H') { //pdd
            for ($row = 2; $row <= $highestRow; $row++) {
                // $data[] = [
                //     'title' => $sheet->getCell('A' . $row)->getValue(),
                //     'order_no' => $sheet->getCell('B' . $row)->getValue(),
                //     'name' => $sheet->getCell('C' . $row)->getValue(),
                //     'tel' => $sheet->getCell('D' . $row)->getValue(),
                //     'area_info' => $sheet->getCell('E' . $row)->getValue() . ' ' . $sheet->getCell('F' . $row)->getValue() . ' ' . $sheet->getCell('G' . $row)->getValue() . ' ' . $sheet->getCell('H' . $row)->getValue()
                // ];
                $data[]=[
                    'title'=>$sheet->getCell('A'.$row)->getValue(),
                    'third_order_no'=>$sheet->getCell('B'.$row)->getValue(),
                    'receive_name'=>$sheet->getCell('C'.$row)->getValue(),
                    'receive_phone'=>$sheet->getCell('D'.$row)->getValue(),
                    'receive_province'=>$sheet->getCell('E'.$row)->getValue(),
                    'receive_city'=>$sheet->getCell('F'.$row)->getValue(),
                    'receive_town'=>$sheet->getCell('G'.$row)->getValue(),
                    'receive_address'=>$sheet->getCell('H'.$row)->getValue(),
                ];
            }
        } elseif ($highestColumn == 'E') {
            // for ($row = 2; $row <= $highestRow; $row++) {
            //     $check_area = $sheet->getCell('C' . $row)->getValue();
            //     $temp_area = explode(' ', $check_area);
            //     if (count($temp_area) != 4){
            //         $this->error('地址格式不正确，请检查后重新导入');
            //     }
            //     $data[] = [
            //         'title' => $sheet->getCell('E' . $row)->getValue(),
            //         'order_no' => $sheet->getCell('A' . $row)->getValue(),
            //         'name' => $sheet->getCell('B' . $row)->getValue(),
            //         'tel' => $sheet->getCell('D' . $row)->getValue(),
            //         'area_info' => $sheet->getCell('C' . $row)->getValue()
            //     ];
            // }
            
            for($row=2; $row<=$highestRow; $row++){
                // $phone = $sheet->getCell('D' . $row)->getValue();
                $data[]=[
                    'title' => $sheet->getCell('E' . $row)->getValue(),
                    'third_order_no'=>$sheet->getCell('A' . $row)->getValue(),
                    'receive_name'=>$sheet->getCell('B' . $row)->getValue(),
                    'receive_phone'=>$sheet->getCell('D' . $row)->getValue(),
                    'area_info' => preg_replace('# #', '', $sheet->getCell('C' . $row)->getValue())
                ];
            }
        } else {
            $this->error('模板不合符规则，请确认后重新上传');
        }
        
        $str = '';
        for ($i = 0; $i < count($data); $i ++) {
            if ($highestColumn == 'H') {
                $str .= $data[$i]['third_order_no'].','.
                    $data[$i]['receive_name'].','.
                    $data[$i]['receive_phone'].','.
                    $data[$i]['receive_province'].$data[$i]['receive_city'].$data[$i]['receive_town'].$data[$i]['receive_address'].
                    PHP_EOL;
            } else {
                $str .= $data[$i]['third_order_no'].','.
                    $data[$i]['receive_name'].','.
                    $data[$i]['receive_phone'].','.
                    $data[$i]['area_info'].
                    PHP_EOL;
            }
        }
        $str = rtrim($str, PHP_EOL);
        $address = htmlspecialchars_decode($str);
        $ret = _disposeAddress($address);
        if ($ret['flag'] == 0) {
            $this->error($ret['msg']);
        }
        foreach ($ret['data'] as &$v) {
            $v['area_info'] = $v['province'].' '.$v['city'].' '.$v['area'].' '.$v['detail'];
            $v['tel'] = $v['phone'];
            unset($v['province'],$v['city'],$v['area'],$v['detail'],$v['phone'],$v['town']);
        }
        $this->success($ret['msg'], $ret['data']);        
        $this->success('ok', $data);
    }

    public function check_handle_list($list = [])
    {

    }
}